package com.ykdl.app.base;

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.ykdl.app.R;
import com.ykdl.app.Utils.NetUtils;
import com.ykdl.app.base.BaseFragment.onNetWorkNoConnectListener;
import com.ykdl.app.bean.NetWorkStateBean;
import com.ykdl.app.view.CustomProgressDialog;

import de.greenrobot.event.EventBus;
/********************************************************************************

** 作者 jiafeiyu
** 时间 2015-6-1
** 描述
**  fragment基础类--基于系统的

*********************************************************************************/
public abstract class  BaseFragments extends Fragment {


	protected RelativeLayout progress = null;
	protected RelativeLayout errorViewLayout = null;
	protected RelativeLayout centerViewLayout = null;
	protected TextView tv_error_text;
	protected View convertView  = null;
	protected ProgressBar loadingView = null;
	public Context mContext;
	private ImageView iv_error_pic;
	private CustomProgressDialog dialog;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		 convertView = inflater.inflate(R.layout.base_fragment_main, null);
		 mContext =  getActivity();
		 dialog =new CustomProgressDialog(mContext, "",R.anim.frame,R.style.Custom_Progress);
			dialog.getWindow().setBackgroundDrawableResource(R.color.transparent); 
		 centerViewLayout = (RelativeLayout) convertView.findViewById(R.id.baseCenterView);
		 errorViewLayout = (RelativeLayout) convertView.findViewById(R.id.error_view);
		 tv_error_text = (TextView) convertView.findViewById(R.id.tv_error_text);
		 loadingView = (ProgressBar) convertView.findViewById(R.id.loading);
		 iv_error_pic = (ImageView) convertView.findViewById(R.id.iv_error_pic);
		 
		 iv_error_pic.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showErrorView(false, null);
				if (NetUtils.isConnectInternet(mContext)) {
					
					tryAgain();
				}else {
					showToast("网络连接不畅，请检查网络设置");
					showErrorView(true, null);
				}
				
			}
		});
		 
		
		 onNewCreateView(convertView);
		 EventBus.getDefault().register(this);
		return convertView;
	}

	
	 /**
     * 
     *
     * @param layoutId
     */
    protected void setCenterView(int layoutId) {
        View view = View.inflate(getActivity(), layoutId, null);
        setCenterView(view);
    }

    /**
     * 
     *
     * @param view
     */
    protected void setCenterView(View view) {
        setCenterView(view, new LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT));
    }

    /**
     * 
     *
     * @param view
     * @param params
     */
    protected void setCenterView(View view, ViewGroup.LayoutParams params) {
        centerViewLayout.addView(view, params);
    }

	protected abstract void onNewCreateView(View view);
	

	protected void setIsShowLoad(boolean flag) {
        if (flag) {
        	dialog.show();
        } else {
        	dialog.dismiss();
        }
    }
	private boolean isShowContent = false;
	protected void showErrorView(boolean flag,String content){
		if (flag) {
			errorViewLayout.setVisibility(View.VISIBLE);
			centerViewLayout.setVisibility(View.GONE);
			if (!TextUtils.isEmpty(content)) {
				
				tv_error_text.setText(content);
			}
			isShowContent = false;
		}else {
			errorViewLayout.setVisibility(View.GONE);
			centerViewLayout.setVisibility(View.VISIBLE);
			isShowContent = true;
		}
	}
	
	protected abstract void tryAgain();
	
	
	public void showToast(String content){
		Toast.makeText(mContext, content, 0).show();
	}
	/**
	 * 已经有网，如果之前没有网，则，重新加载数据
	 * 
	 * @param event
	 */
	public void onEventMainThread(NetWorkStateBean event) {
		if (event != null&&event.isConnect()) {
			if (!isShowContent) {
				showErrorView(false, null);
				setIsShowLoad(true);
				tryAgain();
			}
		}
		if (mConnectListener!=null&&event.isConnect()) {
			mConnectListener.onConnect(false);
		}
		
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}
	
	private onNetWorkNoConnectListener mConnectListener;
	
	
	public void setmConnectListener(onNetWorkNoConnectListener mConnectListener) {
		this.mConnectListener = mConnectListener;
	}
}
